package cn.smbms.service.user;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.user.UserMapper;
import cn.smbms.pojo.User;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Connection;
import java.util.List;


@Transactional
@Service("UserServiceImpl2")
public class UserServiceImpl2 implements UserService {
    @Resource
  private  UserMapper mapper ;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean add(User user) {
        boolean flag = false;
        try {
            int updateRows = mapper.add(user);
            if(updateRows > 0){
                flag = true;
                System.out.println("add success!");
            }else{
                System.out.println("add failed!");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return flag;
    }

    @Override
    public User login(String userCode, String userPassword) {
        User user = null;
        try {
            user = mapper.getLoginUser(userCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //匹配密码
        if (null != user) {
            if (!user.getUserPassword().equals(userPassword))
                user = null;
        }
        return user;
    }
    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        List<User> userList = null;
        System.out.println("queryUserName ---- > " + queryUserName);
        System.out.println("queryUserRole ---- > " + queryUserRole);
        System.out.println("currentPageNo ---- > " + currentPageNo);
        System.out.println("pageSize ---- > " + pageSize);
        try {
            userList = mapper.getUserList(queryUserName,queryUserRole,currentPageNo,pageSize);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {
        int count = 0;
        System.out.println("queryUserName ---- > " + queryUserName);
        System.out.println("queryUserRole ---- > " + queryUserRole);
        try {
            count = mapper.getUserCount(queryUserName,queryUserRole);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public User selectUserCodeExist(String userCode) {
        // TODO Auto-generated method stub
        User user = null;
        try {
            user = mapper.getLoginUser(userCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteUserById(Integer delId) {
        boolean flag = false;
        try {
            if(mapper.deleteUserById(delId) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public User getUserById(String id) {
        User user = null;
        try{
            user = mapper.getUserById(id);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            user = null;
        }
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean modify(User user) {
        // TODO Auto-generated method stub
        boolean flag = false;
        try {
            if(mapper.modify(user) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updatePwd(int id, String pwd) {
        boolean flag = false;
        try{
            if(mapper.updatePwd(id,pwd) > 0)
                flag = true;
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return flag;
    }
}
